package com.wzh.houseEs.controller;

import com.wzh.common.utils.JwtUtil;
import com.wzh.common.utils.R;
import com.wzh.houseEs.service.EsService;
import com.wzh.houseEs.vo.EsVo;
import com.wzh.houseEs.vo.NewHouseVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

/**
 * @author WZH
 * @title: EsController
 * @projectName find_house
 * @description:
 * @date 2022/3/22
 */
@RestController
@RequestMapping("es")
public class EsController {
    @Autowired
    private EsService esService;
    @Autowired
    private JwtUtil jwtUtil;


    /*
     * 置顶操作
     * */
    @RequestMapping("/upup/{houseId}")
    public R upup(@PathVariable("houseId")Integer houseId) throws IOException {
        //首先根据id获取房子信息
        return esService.getById1(houseId);
    }

    /**
     * 发布新房
     */
    @RequestMapping("upNewHouse")
    public R upNewHouse(@RequestBody NewHouseVo newHouseVo){
        return esService.upNewHouse(newHouseVo);
    }

    //租房搜素
    @RequestMapping("/searchlist")
    public R searchlist(@RequestParam Map<String, Object> params){


        EsVo searchlist = esService.searchlist(params);
        return R.ok().put("list",searchlist.getPageUtils()).put("label",searchlist.getHouseDescVo());
    }

    //我发布的租房搜素
    @RequestMapping("/searchlist2")
    public R searchlist2(@RequestParam Map<String, Object> params) throws IOException {
//        String token = request.getHeader("token");
        String token = (String) params.get("token");
        if (token==null){
            return R.error("用户未登录");
        }
        Claims claims = jwtUtil.parseJWT(token);
        if (claims==null||!"user".equals(claims.get("roles"))){
            return R.error("用户未登录");
        }
        Integer user_id = Integer.parseInt(claims.getId());


        EsVo searchlist2 = esService.searchlist2(params,user_id);
        return R.ok().put("list",searchlist2.getPageUtils()).put("label",searchlist2.getHouseDescVo());
    }

}
